/**
 * SysCommentController.java -- 2017-10-20 13:40:09 作者：欧增奇 -- 代码生成器自动生成 联系方式：425752797@qq.com http://www.ouzengqi.com/
 */
package com.ozq.sys.controller.action;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ozq.common.dao.entity.SpringJson;
import com.ozq.common.page.Page;
import com.ozq.common.validator.Jump;
import com.ozq.common.validator.Update;
import com.ozq.sys.controller.form.SysCommentForm;
import com.ozq.sys.dao.entity.SysComment;
import com.ozq.sys.service.inter.SysCommentService;

/**
 * <评论表控制> <功能详细描述>
 * 
 * @author 欧增奇
 * @version [V2.0, 2017年10月20日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping("sys")
public class SysCommentController
{
    static Logger logger = LoggerFactory.getLogger(SysCommentController.class);

    /**
     * 应用-评论表服务
     */
    @Autowired
    private SysCommentService sysCommentService;

    /**
     * 应用-评论表-跳转
     * 
     * @param sysCommentForm
     * @return
     */
    @RequestMapping("jump/sysComment/to")
    public String jumpTo(Model model, @Validated({ Jump.class }) SysCommentForm sysCommentForm, BindingResult result)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysCommentForm:{}", sysCommentForm);
        }

        // 参数验证-不合法直接返回错误的页面或则错误的json
        if (result.hasErrors())
        {
            model.addAttribute("errors", result.getFieldErrors());
            return "common/error";
        }

        return "sysComment/" + sysCommentForm.getJump();
    }

    /**
     * 应用-评论表分页查询列表-执行
     * 
     * @param model
     * @param sysCommentForm
     * @return
     */
    @RequestMapping("listPage/sysComment/do")
    public String listPageSysCommentDo(Model model, SysCommentForm sysCommentForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysCommentForm:{}", sysCommentForm);
        }

        // 基本类
        SysComment sysComment = sysCommentForm.getSysComment();
        if (sysComment == null)
            sysComment = new SysComment();

        // 模糊统计
        int totalCount = sysCommentService.countLike(sysComment);

        // 创建分页
        Page page = sysCommentForm.getPage();
        if (page == null)
            page = new Page();
        page.setTotalCount(totalCount);
        page = sysCommentService.pageManage(page);

        // 模糊分页查询
        model.addAttribute("sysComments", sysCommentService.listPageLike(page, sysComment));
        model.addAttribute("page", page);
        model.addAttribute("sysComment", sysComment);
        return "sysComment/listPageSysComment";
    }

    /**
     * 应用-评论表分页查询列表-执行-异步
     * 
     * @param model
     * @param sysCommentForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ajaxListPage/sysComment/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson ajaxListPageSysCommentDo(Model model, SysCommentForm sysCommentForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysCommentForm:{}", sysCommentForm);
        }

        // 基本类
        SysComment sysComment = sysCommentForm.getSysComment();
        if (sysComment == null)
            sysComment = new SysComment();

        // 模糊统计
        int totalCount = sysCommentService.countLike(sysComment);

        // 创建分页
        Page page = sysCommentForm.getPage();
        if (page == null)
            page = new Page();
        page.setTotalCount(totalCount);
        page = sysCommentService.pageManage(page);

        // 模糊分页查询
        Map<String, Object> map = new HashMap<>();
        map.put("sysComments", sysCommentService.listPageLike(page, sysComment));
        map.put("page", page);
        map.put("sysComment", sysComment);
        return new SpringJson(true, map);
    }

    /**
     * 应用-评论表分页统计条数-执行-异步
     * 
     * @param model
     * @param sysCommentForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ajaxCount/sysComment/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson ajaxCountSysCommentDo(Model model, SysCommentForm sysCommentForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysCommentForm:{}", sysCommentForm);
        }

        // 基本类
        SysComment sysComment = sysCommentForm.getSysComment();
        if (sysComment == null)
            sysComment = new SysComment();

        // 模糊统计
        int totalCount = sysCommentService.countLike(sysComment);
        return new SpringJson(true, totalCount);
    }

    /**
     * 应用-评论表分页查询列表-执行-异步
     * 
     * @param model
     * @param sysCommentForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ajaxList/sysComment/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson ajaxListSysCommentDo(Model model, SysCommentForm sysCommentForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysCommentForm:{}", sysCommentForm);
        }

        // 基本类
        SysComment sysComment = sysCommentForm.getSysComment();
        if (sysComment == null)
            sysComment = new SysComment();

        return new SpringJson(true, sysCommentService.listLike(sysComment));
    }

    /**
     * 应用-评论表修改跳转-执行
     * 
     * @param model
     * @param sysCommentForm
     * @return
     */
    @RequestMapping("update/sysComment/to")
    public String updateSysCommentTo(Model model, @Validated({ Update.class }) SysCommentForm sysCommentForm, BindingResult result)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysCommentForm:{}", sysCommentForm);
        }

        // 参数验证-不合法直接返回错误的页面或则错误的json
        if (result.hasErrors())
        {
            model.addAttribute("errors", result.getFieldErrors());
            return "common/error";
        }

        model.addAttribute("sysComment", sysCommentService.findById(sysCommentForm.getId()));
        return "sysComment/addOrUpdateSysComment";
    }

    /**
     * 应用-评论表修改-执行
     * 
     * @param model
     * @param sysCommentForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "update/sysComment/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson updateSysCommentDo(Model model, SysCommentForm sysCommentForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysCommentForm:{}", sysCommentForm);
        }

        boolean result = sysCommentService.update(sysCommentForm.getSysComment());
        return new SpringJson(result);
    }

    /**
     * 应用-评论表添加-执行
     * 
     * @param model
     * @param sysArticleForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "add/sysComment/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson addSysCommentDo(Model model, SysCommentForm sysCommentForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysCommentForm:{}", sysCommentForm);
        }

        SysComment sysComment = sysCommentService.add(sysCommentForm.getSysComment());
        return new SpringJson(sysComment == null ? false : true, sysComment);
    }
}